import {Base} from '../../utils/base.js';

class Cart extends Base{

    constructor(){
      super();
      this._storageKeyName = 'cart';
    }

    /*
    * 加入到购物车
    * 如果之前没有样的商品，则直接添加一条新的记录， 数量为 counts
    * 如果有，则只将相应数量 + counts
    * @params:
    * item - {obj} 商品对象,
    * counts - {int} 商品数目,
    * */
    add(item,counts){
       var cartData = this.getCartDataFromLocal();
       var isHasInfo = this._isHasThatOne(item.id,cartData);
       if(isHasInfo.index == -1){
         item.counts = counts;
         item.selectStatus = true;
         cartData.push(item);
       }else{
         cartData[isHasInfo.index].counts += counts;
       }
      wx.setStorageSync(this._storageKeyName, cartData)
    }

/**
 * 从缓存中读取购物车数据
 */
    getCartDataFromLocal(flag){
      var res = wx.getStorageSync(this._storageKeyName);
      if(!res){
        return [];
      }
      if (flag) {
        var newRes = [];
        for (let i = 0; i < res.length; i++) {
          if (res[i].selectStatus) {
            newRes.push(res[i]);
          }
        }
        res = newRes;
      }
      return res;

    }

    /**
     * 计算购物车总数量
     * flag true 考虑商品选择状态
     */
    getCartTotalCounts(flag){
      var data = this.getCartDataFromLocal();
      var counts = 0;
      for(let i=0;i<data.length;i++){
        if(flag){
          if(data[i].selectStatus){
            counts += data[i].counts;
          }
        }else{
          counts += parseInt(data[i].counts);
        }
      }
      console.log("--"+counts)
      return counts;
    }

   /*购物车中是否已经存在该商品,并且返回这个商品以及所在数据中的索引*/
    _isHasThatOne(id,arr){
      var item,result={index:-1};
        for(let i=0;i<arr.length;i++){
            item=arr[i];
            if(item.id==id) {
                result = {
                    index:i,
                    data:item
                };
                break;
            }
        }
      return result;
    }
 

  /*
    * 修改商品数目
    * params:
    * id - {int} 商品id
    * counts -{int} 数目
    * */
  _changeCounts(id, counts) {
    var cartData = this.getCartDataFromLocal(),
      hasInfo = this._isHasThatOne(id, cartData);
    if (hasInfo.index != -1) {
      if (hasInfo.data.counts > 1) {
        cartData[hasInfo.index].counts += counts;
      }
    }
    wx.setStorageSync(this._storageKeyName, cartData);
  };


      /*
    * 购物车加
    * */
      addCounts(id) {
        this._changeCounts(id, 1);
      };

      /*
      * 购物车减
      * */
      cutCounts(id) {
        this._changeCounts(id, -1);
      };
  
      delete(ids){
        if(!(ids instanceof Array)){
          ids = [ids];
        }
        var cartData = this.getCartDataFromLocal();
        for(let i=0;i<ids.length;i++){
          var hasInfo = this._isHasThatOne(ids[i],cartData);
          if(hasInfo.index != -1){
            cartData.splice(hasInfo.index,1);
          }
        }
        wx.setStorageSync(this._storageKeyName, cartData);
      }

}

export {Cart};